package com.apestech.framework.xml.sql.node;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.modul.FromList;
import com.apestech.framework.xml.plugin.modul.FromField;
import com.apestech.framework.xml.sql.modul.QueryRule;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.List;
import java.util.Locale;

/**
 * @author hup
 * @create 2018-11-05-17:52
 */
public class NodeRule {

    private Element node;
    private QueryRule rule=new QueryRule();
    private JSONObject froms;

    public NodeRule(String from){
        rule.setFrom(from);
    }

    public NodeRule(Element node){
        this.node=node;
    }

    public QueryRule getRule() {
        return rule;
    }

    public void getAllRule() throws Exception{
        String from=node.attributeValue("from");
        String autoCompletion=node.attributeValue("autoCompletion");
        String page=node.attributeValue("page");
        String expain=node.attributeValue("expain");
        String local=node.attributeValue("local");
        String pageType=node.attributeValue("pageType");
        rule.setBase(from, autoCompletion, page, expain, local, pageType);

        List<Attribute> alist = node.attributes();
        for (Attribute attr : alist) {
            String key=attr.getName().trim();
            String keyl=key.toLowerCase();
            String value=attr.getValue().trim();
            if(keyl.indexOf("from")>=0){
                if(keyl.length()==4){
                    rule.setFrom(value);
                } else {
                    if(froms==null){
                        froms=new JSONObject();
                    }
                    froms.put(keyl, new FromField(value, null));
                }
            }
        }
        if(froms!=null){
            rule.setFroms(froms);
        }

        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element eml=(Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                switch (nname){
                    case "where":{
                        getWhere(eml);
                        break;
                    }
                    case "output":{
                        getOutput(eml);
                        break;
                    }
                }
            }
        }
    }

    private void getWhere(Element e){
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                if("exp".equals(nname)){
                    getExpField(eml);
                } else if ("sameas".equals(nname)){
                    getSameas(eml);
                }
            }
        }
    }

    private void getSameas(Element e){
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String id=eml.getName();
                String alias=eml.attributeValue("alias");
                rule.addSameas(id, alias);
            }
        }
    }

    private void getExpField(Element e){
        String id=e.attributeValue("id");
        String outerExp=e.attributeValue("outerExp");
        String innerExp=e.attributeValue("innerExp");
        rule.setExpAttr(id, outerExp, innerExp);
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                if("fields".equals(nname)){
                    String alias=eml.attributeValue("alias");
                    String field=eml.attributeValue("field");
                    String express=eml.attributeValue("express");
                    rule.addExpField(id, alias, field, express);
                }
            }
        }
    }

    private void getOutput(Element e){
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                if("fields".equals(nname)){
                    String field=eml.attributeValue("field");
                    String format=eml.attributeValue("format");
                    String type=eml.attributeValue("type");
                    String xlsFormat=eml.attributeValue("xlsFormat");
                    String scale=eml.attributeValue("scale");
                    rule.addOutField(field, type, format, xlsFormat, scale);
                }
            }
        }
    }

}
